package tv.acgn.advertisement.publish.dao.dsl;

import java.time.LocalDateTime;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.acgn.advertisement.publish.po.CreativePO;
import tv.acgn.advertisement.publish.po.QCreativePO;
import tv.acgn.advertisement.publish.vo.creative.CreativeRequestVO;

/**
 * @author : junG
 * @program : advertisement-ad-pub-dao
 * @description : 创意用于复杂条件和参数持久化的操作类
 * @date : 2019-06-01 23:52
 **/

@Service
public class CreativeDslDao {

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * <h2>根据条件参数更新创意 CreativePO</h2>
     *
     * @param creativePO [1]
     * @param requestVO  [2]
     *
     * @return {@link Long}
     *
     * @author junG
     * @description 根据条件参数更新创意 CreativePO 为空的字段则不更新
     * @date 0:16 2019/6/2
     */
    public long update(CreativePO creativePO, CreativeRequestVO requestVO) {
        QCreativePO qCreativePO = QCreativePO.creativePO;
        long result = queryFactory.update(qCreativePO)
            .set(qCreativePO.name, requestVO.getName())
            .set(qCreativePO.type, requestVO.getType())
            .set(qCreativePO.materialType, requestVO.getMaterialType())
            .set(qCreativePO.height, requestVO.getHeight())
            .set(qCreativePO.width, requestVO.getWidth())
            .set(qCreativePO.size, requestVO.getSize())
            .set(qCreativePO.duration, requestVO.getDuration())
            .set(qCreativePO.auditStatus, requestVO.getAuditStatus())
            .set(qCreativePO.URI, requestVO.getURI())
            .set(qCreativePO.updateTime, LocalDateTime.now())
            .execute();
        return result;
    }
}
